package org.iweb.ui.company;

import java.util.HashMap;
import java.util.Map;

import org.iweb.init.AbstractRegister;
import org.iweb.init.Register;
import org.iweb.init.scanner.AnnotationScanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Register(name = "CompanyRegister")
public class CompanyRegister extends AbstractRegister {

	Logger log = LoggerFactory.getLogger(CompanyRegister.class);

	private Map<Company, Class> companies;

	public void addCompany(Company ann, Class clazz) {
		Class c = getCompanies().get(ann);
		if (c == null) {
			getCompanies().put(ann, clazz);
			log.debug("Register company " + clazz.getName() + " on path: " + ann.hostname());
		}
	}

	public Map<Company, Class> getCompanies() {
		if (companies == null)
			companies = new HashMap<Company, Class>();
		return companies;
	}

	@Override
	public void populate(AnnotationScanner scanner) {
		Map<String, Object> annotatedClasses = scanner.getClasses().get(
				Company.class.getName());

		if (annotatedClasses != null) {
			Company ann;
			Class c;
			for (Object obj : annotatedClasses.values()) {

				if (obj instanceof Class) {
					c = (Class) obj;
					ann = (Company) c.getAnnotation(Company.class);					
					addCompany(ann, c);
				}
			}
		}

	}

	@Override
	public void clear() {
		getCompanies().clear();		
	}

	@Override
	public Long getNumReg() {
		return new Long(getCompanies().size());
	}
}
